<?php
namespace app\common\customer\logic;

use app\common\BaseLogic;
use app\common\customer\model\CustomerAllocate;

class AllocateLogic extends BaseLogic
{
    public function getAllocate($memberId, $userId, $fields="*") {
        $cacheName = "member:allocate:{$memberId}:{$userId}";

        if ($fields == "*") {
            $data = $this->redis->hGetAll($cacheName);
        } else {
            $data = $this->redis->hMGet($cacheName, $fields);
        }

        if (empty($data['id'])) {
            $model = new CustomerAllocate();
            $row = $model->where('member_id', $memberId)->where('create_id', $userId)->find();
            if (!empty($row)) {
                $data = $row->getData();
                $this->redis->hMSet($cacheName, $data);
                if ($fields != "*") $data = array_search_keys($data, $fields);
            } else {
                $data = [];
            }
        }

        return $data;
    }

    public function resetAllocateCache($memberId, $userId){
        $cacheName = "member:allocate:{$memberId}:{$userId}";
        $this->redis->del($cacheName);
    }

    // 部门平均分配
    public function department($memberId)
    {

    }

    // 组员间平均分配
    public function staff()
    {

    }
}